package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.mapper.PermissionMapper;
import com.itany.pojo.Permission;
import com.itany.service.PermissionService;
import com.itany.pojo.Menu;
import com.itany.utils.StringUtils;
import com.itany.vo.PermissionVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class PermissionServiceImpl implements PermissionService {

    @Resource
    private PermissionMapper permissionMapper;

    @Override
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public TreeSet<Menu> findMenu(Integer parentid) {
        TreeSet<Menu> menus = permissionMapper.selectMenu(0);
        for(Menu menu : menus){
            menu.setIcon("icon-sys");
            for(Menu m : menu.getMenus()){
                m.setIcon("icon-nav");
            }
        }
        return menus;
    }

    @Override
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public PageInfo<Permission> findByFuzzy(Permission permission,Integer page,Integer rows) {
        permission.setName(StringUtils.formatLike(permission.getName()));
        permission.setSname(StringUtils.formatLike(permission.getSname()));
        PageHelper.startPage(page,rows);
        List<Permission> permissions = permissionMapper.selectByFuzzy(permission);
        PageInfo<Permission> permissionPageInfo = new PageInfo<Permission>(permissions);
        return permissionPageInfo;
    }

    @Override
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public List<Permission> findParents() {
        List<Permission> permissions = permissionMapper.selectParents();
        return permissions;
    }

    @Override
    public void modifyById(Permission permission) {

        permissionMapper.updateById(permission);
    }
}
